#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<wait.h>
#include<sys/types.h>

void test1()
{
  int fd[2]={0};
  if(pipe(fd)<0)
  {
    perror("pipe");
    return ;
  }

  pid_t id=fork();

  if(id==0)//child
  {
    close(fd[0]);
    const char* msg="hello father ,I am child....\n";
    int count=10;
    while(count--)
    {
      write(fd[1],msg,strlen(msg));
      sleep(1);
    }
    close(fd[1]);
    exit(1);
  }

  close(fd[1]);
  char buf[64];

  while(1)
  {
    ssize_t s =read(fd[0],buf,sizeof(buf));
    if(s>0)
    {
      buf[s]='\0';
      printf("child send to father:%s\n",buf);
    }
    else if(s==0)
    {
      printf("read file end\n");
      break;
    }
    else 
    {
      printf("read fail\n");
      break;
    }

  }

  close(fd[0]);
  waitpid(id,NULL,0);
}

int main()
{

  test1();

   
  return 0;
}
